package CreatorPattern.Builder;

/**
 * @author: Themberfue
 * @date: 2025/8/10 11:33
 * @description:
 */
// 1. 产品类 - 电脑
class Computer {
    private String cpu;       // 处理器
    private String memory;    // 内存
    private String hardDisk;  // 硬盘
    private String graphics;  // 显卡
    private String monitor;   // 显示器
    private String os;        // 操作系统

    // 私有构造函数，只能通过建造者创建
    private Computer(Builder builder) {
        this.cpu = builder.cpu;
        this.memory = builder.memory;
        this.hardDisk = builder.hardDisk;
        this.graphics = builder.graphics;
        this.monitor = builder.monitor;
        this.os = builder.os;
    }

    // 显示电脑配置
    public void showConfiguration() {
        System.out.println("电脑配置:");
        System.out.println("CPU: " + cpu);
        System.out.println("内存: " + memory);
        System.out.println("硬盘: " + hardDisk);
        System.out.println("显卡: " + graphics);
        System.out.println("显示器: " + monitor);
        System.out.println("操作系统: " + os);
        System.out.println("---------------------");
    }

    // 2. 内部建造者类（支持链式调用）
    public static class Builder {
        private String cpu;
        private String memory;
        private String hardDisk;
        private String graphics;
        private String monitor;
        private String os;

        // 链式设置方法，返回Builder自身
        public Builder cpu(String cpu) {
            this.cpu = cpu;
            return this;
        }

        public Builder memory(String memory) {
            this.memory = memory;
            return this;
        }

        public Builder hardDisk(String hardDisk) {
            this.hardDisk = hardDisk;
            return this;
        }

        public Builder graphics(String graphics) {
            this.graphics = graphics;
            return this;
        }

        public Builder monitor(String monitor) {
            this.monitor = monitor;
            return this;
        }

        public Builder os(String os) {
            this.os = os;
            return this;
        }

        // 构建产品
        public Computer build() {
            // 可以在这里添加参数校验
            if (cpu == null || memory == null || hardDisk == null) {
                throw new IllegalArgumentException("CPU、内存和硬盘为必填配置");
            }
            return new Computer(this);
        }
    }

    // 3. 预设配置的静态方法（可选）
    public static Builder gamingComputer() {
        return new Builder()
                .cpu("Intel Core i9")
                .memory("32GB DDR5")
                .hardDisk("1TB NVMe SSD")
                .graphics("NVIDIA RTX 4090")
                .monitor("27英寸 4K 144Hz")
                .os("Windows 11 专业版");
    }

    public static Builder officeComputer() {
        return new Builder()
                .cpu("Intel Core i5")
                .memory("16GB DDR4")
                .hardDisk("512GB SSD")
                .graphics("集成显卡")
                .monitor("24英寸 1080P")
                .os("Windows 11 家庭版");
    }
}

// 4. 客户端代码
public class BuilderExample {
    public static void main(String[] args) {
        // 1. 使用预设配置构建游戏电脑
        System.out.println("=== 预设游戏电脑 ===");
        Computer gamingPc = Computer.gamingComputer().build();
        gamingPc.showConfiguration();

        // 2. 使用预设配置构建办公电脑，并修改部分配置
        System.out.println("=== 定制办公电脑 ===");
        Computer customOfficePc = Computer.officeComputer()
                .memory("32GB DDR4")  // 升级内存
                .hardDisk("1TB SSD")  // 升级硬盘
                .build();
        customOfficePc.showConfiguration();

        // 3. 完全自定义配置
        System.out.println("=== 完全自定义电脑 ===");
        Computer fullyCustomPc = new Computer.Builder()
                .cpu("AMD Ryzen 7")
                .memory("64GB DDR5")
                .hardDisk("2TB NVMe SSD")
                .graphics("AMD Radeon RX 7900 XT")
                .monitor("32英寸 4K 165Hz")
                .os("Linux Ubuntu 22.04")
                .build();
        fullyCustomPc.showConfiguration();
    }
}

